package com.chg.mymvc.servlet;

import com.chg.mymvc.entity.User;
import com.chg.mymvc.service.UserServiceImpl;
import com.chg.mymvc.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/amanage.html")
public class AMangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        UserServiceImpl us = new UserServiceImpl();




        //分页
        //获取用户提交的当前页码
int pagesize=5;
String num=req.getParameter("pagenum");
int pagenum=num==null?1:Integer.parseInt(num);
//int page=1;
//if(num!=null){
//    page=Integer.parseInt(num);
//}
//创建分页工具
        Page page = new Page();
        page.setPageNum(pagenum);
        page.setPageSize(pagesize);
//获取总条数
        page.setTotal(us.allcount());

//传list为了展示
        List<User> list = us.queryAll(page.getPageNum(), page.getPageSize());

       page.setList(list);
        req.setAttribute("list",list);
        req.setAttribute("page",page);
        req.getRequestDispatcher("page/AccountManagement.jsp").forward(req,resp);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
